package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.EmergencyResourceEdit;
import com.deyuanyun.pic.ae.controller.dto.EmergencyResourceQuery;
import com.deyuanyun.pic.ae.controller.dto.SewerQuery;
import com.deyuanyun.pic.ae.domain.EmergencyResource;
import com.deyuanyun.pic.ae.domain.EmergencyResourceExample;
import com.deyuanyun.pic.ae.domain.Sewer;
import com.deyuanyun.pic.ae.domain.SewerExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.mapper.EmergencyResourceMapper;
import com.deyuanyun.pic.ae.mapper.SewerMapper;
import com.deyuanyun.pic.ae.mapper.dao.EmergencyResourceDao;
import com.deyuanyun.pic.ae.mapper.dao.SewerDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.AEPipeLineService;
import com.deyuanyun.pic.ae.service.EmergencyResourceService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “管道沿线应急资源信息” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-9
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class EmergencyResourceServiceImpl extends AEBaseService implements EmergencyResourceService {

    @Resource
    private EmergencyResourceMapper emergencyResourceMapper;
    @Resource
    private EmergencyResourceDao emergencyResourceDao;

    @Override
    public Page<EmergencyResource> query(EmergencyResourceQuery emergencyResourceQuery) {
        return emergencyResourceDao.query(emergencyResourceQuery);
    }

    @Override
    public EmergencyResource find(String id) {
        return emergencyResourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(EmergencyResource emergencyResource, List<String> imgs) {

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(emergencyResource.getId()) ? emergencyResource.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(emergencyResource.getId())){
            EmergencyResourceExample emergencyResourceExample = new EmergencyResourceExample();
            emergencyResourceExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andNameEqualTo(emergencyResource.getName());
            int countNum = emergencyResourceMapper.countByExample(emergencyResourceExample);
            Asserts.assertLogic(countNum > 0, "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 已经存在，不能重复插入");

            emergencyResource.setId(bizzId);
            emergencyResource.setCreateDate(new Date());
            emergencyResource.setCreateUser(realName);
            emergencyResource.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            emergencyResource.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return emergencyResourceMapper.insertSelective(emergencyResource);
        }

        EmergencyResource emergencyResourceObj = this.find(emergencyResource.getId());
        Asserts.assertLogic(emergencyResourceObj == null, "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 不存在，不允许修改");
        Asserts.assertLogic(isAuditReleased(emergencyResourceObj.getDataStated()), "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 已审核通过,不允许编辑");
        if (isAuditDismissed(emergencyResourceObj.getDataStated())){
            emergencyResource.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return emergencyResourceMapper.updateByPrimaryKeySelective(emergencyResource);
    }

    @Override
    public int audit(EmergencyResource emergencyResource) {
        EmergencyResource emergencyResourceObj = this.find(emergencyResource.getId());
        Asserts.assertLogic(emergencyResourceObj == null, "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 不存在，不允许修改");
        Asserts.assertLogic(isAudit(emergencyResourceObj.getDataStated()), "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 已审核,不允许重复审核");
        return emergencyResourceMapper.updateByPrimaryKeySelective(emergencyResource);
    }

    @Override
    public int delete(String id) {
        EmergencyResource emergencyResource = this.find(id);
        Asserts.assertLogic(emergencyResource == null, "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 不存在，可能已经被删除");
        Asserts.assertLogic(isAuditReleased(emergencyResource.getDataStated()), "名称为:“"+emergencyResource.getName()+"”管道沿线应急资源 已审核通过,不能“删除”");
        emergencyResource = new EmergencyResource();
        emergencyResource.setId(id);
        emergencyResource.setDeleted(Short.valueOf("1"));
        emergencyResource.setUpdateDate(new Date());
        emergencyResource.setUpdateUser(realName);
        emergencyResource.setUpdateRemark("删除");
        return emergencyResourceMapper.updateByPrimaryKeySelective(emergencyResource);
    }

}
